// pages/salesmans/salesmanPerformance/salesmanPerformance.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    salesman_id: '',
    page: 1,
    page_count: 1,
    data_list: [],
    empty: false,
    loadmore: false,
    nomore: false,
    sum_data: '',//统计数据
    isFilter: false,
    drawer: false,
    conditions: { //存放搜索条件
      start_time: '', //开始时间
      end_time: '', //结束时间
      card_number: "",
      village_name:'',
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      salesman_id: options.id
    })
    that.salesmanStatistics();
  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var page = that.data.page + 1;
    if (page > that.data.page_count) {
      that.setData({
        noMore: true
      })
      return false;
    }
    that.setData({
      page: page
    })
    that.salesmanStatistics();
  },
  onPullDownRefresh: function () {
    this.reloadData();
  },

  salesmanStatistics() {
    let conditions = this.data.conditions;
    var that = this;
    app.ajax({
      url: 'Partner/WaterCard/salesmanStatistics',
      method: "POST",
      data: {
        page: that.data.page,
        row: 10,
        salesman_id: that.data.salesman_id,
        start_time: conditions.start_time,
        end_time: conditions.end_time,
        card_number: conditions.card_number,
        village_name:conditions.village_name
      },
      success: function (res) {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        var flag = res.data;
        if (flag.code > 0) {
          var list = [];
          if (that.data.page==1){
            list = flag.data.data;
          } else {
            list = that.data.data_list.concat(flag.data.data);
          }
          that.setData({
            data_list: list,
            sum_data: flag.data.sum_data,
            page_count: flag.data.page.page_count,
            empty: flag.data.page.page_count == 0 ? true : false,
            loadmore: flag.data.page.page_count > list.length ? true : false,
            nomore: flag.data.page.page_count <= list.length ? true : false,
            drawer: false,
          });
        }
      },
      error: e => {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        app.showToast("网络错误");
      }
    })
  },
  // 查看详情
  checkit: function (e) {
    wx.navigateTo({
      url: '../achievementDes/achievementDes?id=' + e.currentTarget.dataset.id,
      success: function (res) {},
      fail: function (res) {},
      complete: function (res) {},
    })
  },

  showDrawer() {
    this.condition = {
      ...this.data.conditions
    };
    this.setData({
      drawer: true,
    })
  },
  closeDrawer() {
    this.setData({
      drawer: false,
      conditions: this.condition
    })
    delete (this.condition)
  },

  handleInput(e) {
    const name = e.currentTarget.dataset.name;
    this.setData({
      [`conditions.${name}`]: e.detail.value
    })
  },
  handleTime(e) {
    const name = e.currentTarget.dataset.name;
    this.setData({
      [`conditions.${name}`]: e.detail.value
    })
  },

  resetDrawer() {
    const conditions = this.data.conditions;
    for (let x in conditions) {
      conditions[x] = "";
    }
    this.setData({
      conditions,
    })
  },
  confirmDrawer() {
    let isFilter = false;
    const conditions = this.data.conditions;
    for (let x in conditions) {
      conditions[x] !== "" && (isFilter = true);
    }
    this.setData({
      isFilter
    })
    delete (this.condition);
    this.reloadData();
  },

  reloadData() {
    this.setData({
      page: 1,
      data_list: [],
      nomore: false,
      loadmore: false,
    })
    wx.showLoading();
    this.salesmanStatistics();
  },
})